<?php
/*************************************************************************

  $Id: IclearSOAP.php 257 2009-06-16 08:57:02Z dis $

  iclear payment system - because secure is simply secure
  http://www.iclear.de

  Copyright (c) 2001 - 2009 iclear

  Released under the GNU General Public License

************************************************************************

                    All rights reserved.

  This program is free software licensed under the GNU General Public License (GPL).

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  USA

*************************************************************************/

	class IclearSOAP extends IclearBase {
	  
	  var $client = false;
	  
	  var $proxy = false;
	  
	  var $url = array();
	  
	  var $credentials = false;
	  
		var $order = false;
		
		var $result = array();
	
	  var $requestID = '';
	  
	  var $orderMethod = '';
	  
	  function IclearSOAP(&$icCore) {
	  	$this->icVersion = '$Id: IclearSOAP.php 257 2009-06-16 08:57:02Z dis $';
	    parent::IclearBase($icCore);
	  }
	  
	  function flush() {
	  	$this->result = array();
	  	$this->requestID = 0;
	  }
	
	  function init() {
	    $this->localEndpointURI();
	  }
	  
	  function initServer() {
	  	$this->server = false;
	  }
	
	  function initClient($uri) {
	    $this->client = false;
	    
	    if($uri) {
	      if($client = $this->icCore->getSoapClient()) {
	        $this->client = $client->getClient($uri);
	      }
	    }
	    
	    return $this->client ? true : false;
	  }

    /**
     * Check if a connection error occured
     *
     * @return string errorMsg
     */
    function soapError() {
      $rc = '';
      if(is_object($this->proxy) && ($rc = $this->proxy->getError())) {
        $this->addError('SOAP proxy: ' . $rc);
      }
      return $rc;
    }
    
   function perform($function = '', $params = false) {
     return $this->icCore->perform($this->client, $function, $params);
   }

   function serviceURL($type = '', $url = '') {
   	$rv = '';
     if($type) {
     	if($url) {
         $this->url[$type] = $url;
       }
       if(isset($this->url[$type])) {
       	$rv = $this->url[$type];
       }
     	
     }
     return $rv;
   }

  function requestID($reqID = '') {
  	if($reqID) {
  		$this->requestID = $reqID;
  	}
  	return $this->requestID;
  }
  
  function submitRequest($function, $params = false) {
  	$res = false;
    $icCore =& $this->icCore;
    $basket =& $icCore->getBasket();

// check if result is already present
    if(isset($this->result[$function]) && $this->result[$function]) {
    	$res = $this->result[$function];
    } else {
// debug mode: use
      if($icCore->debug()) {
	      $params[IC_SOAP_SESSION_ID] = base64_encode($basket->sessionID() . '@' . $icCore->getBaseURL() . 'iclear.php?wsdl');
	      if($this->initClient(IC_URI_DEBUG_ORDER)) {
	        $res = $this->perform($function, array_values($params));
	        $params[IC_SOAP_SESSION_ID] = $basket->sessionID();
	      }
// use parallel request mode - reset SOAP client to live system
        if($icCore->debug() == 2) {
          $this->initClient(IC_URI_ORDER);
          $res = false;
        }
	    } else {
	      $this->initClient(IC_URI_ORDER);
	    }

     // $res is populated if IC_DEBUG == 1 (direct endpoint debugger)
	     if(!$res) {
	       $res = $this->perform($function, array_values($params));
	     }
     
	     $this->result[$function] = $res;
       if(IC_DEBUG_LOG && IC_DEBUG_LOG_FILE) {
	       if($fp = @fopen($this->icCore->getPath('iclear') . IC_DEBUG_LOG_FILE, 'w')) {
	         fputs($fp, date('Y-m-d H:i:s') . "\n");
	         if(method_exists($this->client, '__getLastRequest')) {
		         fputs($fp, "REQUEST:\n" . $this->client->__getLastRequest() . "\n");
		         fputs($fp, "RESPONSE:\n" . $this->client->__getLastResponse() . "\n");
	         }
	         fclose($fp);
	       }
	     }
     }
     
     return $res;
   }
  
  /* Implemented SOAP operations follows */
   
  function sendOrder () {
    $rc = false;
    $proxy =& $this->icCore->getProxy();
    if(! ($shopID = $proxy->perform('shopID')) ) {
      $this->addError('No ShopID found!');
    } elseif(!preg_match('/^[0-9]+$/', $shopID)) {
    	$this->addError('Iclear ShopID not numerical!');
    } elseif (! ($proxy->perform('getOrder')) ) {
      $this->addError('Order not present!');
    } elseif ( !$proxy->perform('orderItemCount') ) {
      $this->addError('Order has no items!');
    } else {
    	$icCore =& $this->icCore;
    	$basket =& $icCore->getBasket();
    	
    	$basket->processOrder();
      
    	
    	$params = array (
        IC_SOAP_SHOP_ID => $shopID,
        IC_SOAP_BASKET_ID => $basket->basketID(),
        IC_SOAP_CURRENCY_ISO => $basket->currency(),
        IC_SOAP_BASKET_ITEMS => $basket->basketItems(),
        IC_SOAP_SESSION_ID => $basket->sessionID(),
        IC_SOAP_LANGUAGE_ISO => $basket->language(),
      );
      
      $res = $this->submitRequest(__FUNCTION__, $params);
      $rc = $basket->submitOK($res);
    }
    return $rc;
  }
  
  function sendOrderB2C () {
    $rc = false;
    $proxy =& $this->icCore->getProxy();
    if(! ($shopID = $proxy->perform('shopID')) ) {
      $this->addError('No ShopID found!');
    } elseif(!preg_match('/^[0-9]+$/', $shopID)) {
    	$this->addError('Iclear ShopID not numerical!');
    } elseif (! ($proxy->perform('getOrder')) ) {
      $this->addError('Order not present!');
    } elseif ( !$proxy->perform('orderItemCount') ) {
      $this->addError('Order has no items!');
    } else {
    	$icCore =& $this->icCore;
    	$basket =& $icCore->getBasket();
    	$basket->processOrder();
      
    	
    	$params = array (
        IC_SOAP_SHOP_ID => $shopID,
        IC_SOAP_SESSION_ID => $basket->sessionID(),
        IC_SOAP_BASKET_ID => $basket->basketID(),
        IC_SOAP_CURRENCY_ISO => $basket->currency(),
        IC_SOAP_LANGUAGE_ISO => $basket->language(),
        IC_SOAP_DELIVERY_ADDRESS => $basket->deliveryAddress->address(),
        IC_SOAP_BASKET_ITEMS => $basket->basketItems(),
      );
      $res = $this->submitRequest(__FUNCTION__, $params);
      $rc = $basket->submitOK($res);
    }
    return $rc;
  }
  
  
}



?>